﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Web.Controls;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web;

namespace Jayden.Site.PopjesInDeZak.Dll
{
	public class PopjesInDeZakPageHtml : PageHtml
	{
		private A m_FrLink;
		private A m_NlLink;
		private A m_EnLink;

		private A m_CartItemCount;
		private Img m_CartImage;

		private Div m_Body;

		private A m_HomeLink;
		private Img m_HomeImage;
		private A m_PopjesLink;
		private Img m_PopjesImage;
		private A m_AboutLink;
		private Img m_AboutImage;
		private A m_PressLink;
		private Img m_PressImage;
		private A m_ContactLink;
		private Img m_ContactImage;
		private A m_FacebookLink;
		private Img m_FacebookImage;
		private A m_ConditionsLink;
		private A m_ConfidentialityLink;

		private Span m_Year;

		public override void Load(System.Xml.XmlNode node, Jayden.Dll.Web.HtmlControlLoader loader)
		{
			base.Load(node, loader);

			m_Year = GetElementByServerId<Span>("year");
			m_Year.InnerText = DateTime.Today.Year.ToString();

			m_FrLink = GetElementByServerId<A>("frLink", true);
			m_NlLink = GetElementByServerId<A>("nlLink", true);
			m_EnLink = GetElementByServerId<A>("enLink", true);

			m_CartItemCount = GetElementByServerId<A>("cartItemCount", true);
			m_CartImage = GetElementByServerId<Img>("cartImage", true);

			m_Body = GetElementByServerId<Div>("body", true);

			m_HomeLink = GetElementByServerId<A>("homeLink", true);
			m_HomeImage = GetElementByServerId<Img>("homeImage", true);
			m_PopjesLink = GetElementByServerId<A>("popjesLink", true);
			m_PopjesImage = GetElementByServerId<Img>("popjesImage", true);
			m_AboutLink = GetElementByServerId<A>("aboutLink", true);
			m_AboutImage = GetElementByServerId<Img>("aboutImage", true);
			m_PressLink = GetElementByServerId<A>("pressLink", true);
			m_PressImage = GetElementByServerId<Img>("pressImage", true);
			m_ContactLink = GetElementByServerId<A>("contactLink", true);
			m_ContactImage = GetElementByServerId<Img>("contactImage", true);
			m_FacebookLink = GetElementByServerId<A>("facebookLink", true);
			m_FacebookImage = GetElementByServerId<Img>("facebookImage", true);
			m_ConditionsLink = GetElementByServerId<A>("conditionsLink", true);
			m_ConfidentialityLink = GetElementByServerId<A>("confidentialityLink", true);

		}

		public override void ProcessHtmlRequest(Jayden.Dll.Web.HttpHandlerContext context)
		{
			ProcessRequestWithContext(context);
			base.ProcessHtmlRequest(context);
		}

		public void ProcessRequestWithContext(Jayden.Dll.Web.HttpHandlerContext context)
		{
			m_Context = context;
			string command = context.Request["command"];
			if (!string.IsNullOrWhiteSpace(command))
			{
				if (command == "content")
				{
					string html = m_Body.ToInnerHtml(context.Translator);
					if (context.Json != null)
						context.Json.Add("content", html);
				}
				else if (command == "addCart")
				{
					CartSessionLine line = new CartSessionLine();
					line.Reference = context.Request["reference"];
					line.Label = context.Request["label"];
					line.Price = 29.90f;
					line.Amount = 1;
					Cart.Lines.Add(line);
					if (context.Json != null)
						context.Json.Add("itemCount", string.Format("{0} popjes", Cart.Lines.Count));
				}
			}




			if (context.Lang == "fr")
				m_FrLink.Classes.Add("selected");
			else if (context.Lang == "nl")
				m_NlLink.Classes.Add("selected");
			else
				m_EnLink.Classes.Add("selected");

			int countPopjes = Cart.Lines.Count;
			string text = string.Format("{0} popjes", countPopjes);
			if(countPopjes == 1)
				text = "1 popje";
			m_CartItemCount.InnerText = text;

			string path = context.Request.Path;
			if (path.StartsWith("/edit/"))
				Body.Classes.Add("edit");
			switch (path)
			{
				case "/":
				case "/default.html":
					m_HomeImage.Src = "/Images/menu.home.on.png";
					Body.Classes.Add("default");
					break;
				case "/popjes.html":
				case "/details.html":
					m_PopjesImage.Src = "/Images/menu.popjes.on.png";
					Body.Classes.Add("popjes");
					break;
				case "/about.html":
					m_AboutImage.Src = "/Images/menu.about.on.png";
					Body.Classes.Add("about");
					break;
				case "/contact.html":
					m_ContactImage.Src = "/Images/menu.contact.on.png";
					Body.Classes.Add("contact");
					break;
				case "/cart.html":
				case "/paypal.html":
				case "/payment.html":
				case "/validate.html":
					m_CartImage.Src = "/Images/cart.on.png";
					Body.Classes.Add("cart");
					break;
				case "/press.html":
					//m_PressImage.Src = "/Images/menu.press.on.png";
					Body.Classes.Add("press");
					break;
				case "/facebook.html":
					//m_FacebookImage.Src = "/Images/menu.facebook.on.png";
					Body.Classes.Add("facebook");
					break;
			}

		}

		private CartSession m_Cart;
		public CartSession Cart
		{
			get
			{
				if (m_Cart == null)
				{
					m_Cart = Context.Context.Session["Cart"] as CartSession;
					if (m_Cart == null)
					{
						m_Cart = new CartSession();
						Context.Context.Session["Cart"] = m_Cart;
					}
				}
				return m_Cart;
			}
		}

		public override void ProcessJsonRequest(Jayden.Dll.Web.HttpHandlerContext context)
		{
			base.ProcessJsonRequest(context);
			ProcessRequestWithContext(context);
		}

		public override void ProcessJsRequest(Jayden.Dll.Web.HttpHandlerContext context)
		{

			context.Js["MenuImages"].Append(@"
	jayden.sfn.preload([
		'/Images/menu.home.on.png',
		'/Images/menu.popjes.on.png',
		'/Images/menu.about.on.png',
		'/Images/menu.contact.on.png',
		'/Images/cart.on.png'
	]);
");

			context.Js["PopjesInDeZak"].AppendOnInit(@"

	JAYDEN.fullAjax = false;

	var $homeLnk = $('#" + m_HomeLink.Id + @"');
	var $popjesLnk = $('#" + m_PopjesLink.Id + @"');
	var $aboutLnk = $('#" + m_AboutLink.Id + @"');
	var $pressLnk = $('#" + m_PressLink.Id + @"');
	var $contactLnk = $('#" + m_ContactLink.Id + @"');
	var $facebookLnk = $('#" + m_FacebookLink.Id + @"');
	var $conditionsLnk = $('#" + m_ConditionsLink.Id + @"');
	var $confidentialityLnk = $('#" + m_ConfidentialityLink.Id + @"');

	var $homeImg = $('#" + m_HomeImage.Id + @"');
	var $popjesImg = $('#" + m_PopjesImage.Id + @"');
	var $aboutImg = $('#" + m_AboutImage.Id + @"');
	var $pressImg = $('#" + m_PressImage.Id + @"');
	var $contactImg = $('#" + m_ContactImage.Id + @"');
	var $facebookImg = $('#" + m_FacebookImage.Id + @"');
	var $cartImg = $('#" + m_CartImage.Id + @"');
	var $body = $('#" + m_Body.Id + @"');

	var $cartItemCount = $('#" + m_CartItemCount.Id + @"');

	JAYDEN.ajax.handlers.push({
		handle: function(data, textStatus, jqXHR) {
			if(data.itemCount)
				$cartItemCount.text(data.itemCount);
			if(data.content)
				$body.html(data.content);
		}
	});

	$homeImg.mouseenter(function(){ $homeImg.data('old-src', $homeImg.attr('src')); $homeImg.attr('src', '/Images/menu.home.on.png'); });
	$homeImg.mouseleave(function(){ $homeImg.attr('src', $homeImg.data('old-src')); });

	$popjesImg.mouseenter(function(){ $popjesImg.data('old-src', $popjesImg.attr('src')); $popjesImg.attr('src', '/Images/menu.popjes.on.png'); });
	$popjesImg.mouseleave(function(){ $popjesImg.attr('src', $popjesImg.data('old-src')); });

	$aboutImg.mouseenter(function(){ $aboutImg.data('old-src', $aboutImg.attr('src')); $aboutImg.attr('src', '/Images/menu.about.on.png'); });
	$aboutImg.mouseleave(function(){ $aboutImg.attr('src', $aboutImg.data('old-src')); });

	$contactImg.mouseenter(function(){ $contactImg.data('old-src', $contactImg.attr('src')); $contactImg.attr('src', '/Images/menu.contact.on.png'); });
	$contactImg.mouseleave(function(){ $contactImg.attr('src', $contactImg.data('old-src')); });

	$cartImg.mouseenter(function(){ $cartImg.data('old-src', $cartImg.attr('src')); $cartImg.attr('src', '/Images/cart.on.png'); });
	$cartImg.mouseleave(function(){ $cartImg.attr('src', $cartImg.data('old-src')); });

	var ajaxContent = function(url) {
		$body.html('');
		return JAYDEN.sfn.submitAjaxData({
			format: 'json',
			command: 'content'
		}, url, 'post');
	};

	if(JAYDEN.fullAjax) {
		$homeLnk.click(function() { return ajaxContent('/default.html'); });
		$popjesLnk.click(function() { return ajaxContent('/popjes.html'); });
		$aboutLnk.click(function() { return ajaxContent('/about.html'); });
		$contactLnk.click(function() { return ajaxContent('/contact.html'); });
		$pressLnk.click(function() { return ajaxContent('/press.html'); });
		$facebookLnk.click(function() { return ajaxContent('/facebook.html'); });
		$confidentialityLnk.click(function() { return ajaxContent('/confidentiality.html'); });
		$conditionsLnk.click(function() { return ajaxContent('/conditions.html'); });
	}
");

			base.ProcessJsRequest(context);
		}
	}
}
